﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using SZHL.UI.AppCode;
using FastReflectionLib;
using SZHL.Data;
using Newtonsoft.Json;

namespace SZHL.UI.API
{
    public class GYSGLManage : IWsService
    {
        public void ProcessRequest(HttpContext context, ref Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            MethodInfo methodInfo = typeof(GYSGLManage).GetMethod(msg.Action.ToUpper());
            GYSGLManage model = new GYSGLManage();
            methodInfo.FastInvoke(model, new object[] { context, msg, P1, P2, UserInfo });
        }
          /// <summary>
        /// 添加供应商
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void ADDGYSGL(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            KZ_GYSGL gys = JsonConvert.DeserializeObject<KZ_GYSGL>(P1);
            if (gys.LinkUser.Trim() == "")
            {
                msg.ErrorMsg = "联系人不能为空";
                return;
            }
            if (gys.GSName.Trim() == "")
            {
                msg.ErrorMsg = "公司名称不能为空";
                return;
            }
            if (gys.ID == 0)
            {
                gys.CRDate = DateTime.Now;
                gys.CRUser = UserInfo.User.UserName;
                new KZ_GYSGLB().Insert(gys);
            }
            else
            {
                new KZ_GYSGLB().Update(gys);
            }
            msg.Result = gys;
        }
        /// <summary>
        /// 获取供应商列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg">公告类型</param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void GETGYSGLLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strWhere = string.Format("  Where 1=1");
            if (P1 != "")
            {
                strWhere += string.Format(" And   LeiBie='{0}'", P1);
            }
            if (P2 != "")
            {
                strWhere += string.Format(" And (GSName  like '%{0}%' or LinkUser  like '%{0}%')", P2);
            }
            string strSql = string.Format("select * from KZ_GYSGL " + strWhere +" ORDER BY ID DESC ");
            msg.Result = new KZ_GYSGLB().GetDTByCommand(strSql);

        }
        /// <summary>
        /// 获取供应商信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg">公告类型</param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void GETGYSGLMODEL(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            msg.Result = new KZ_GYSGLB().GetEntity(d => d.ID.ToString() == P1); 
        }
            /// <summary>
        /// 删除供应商
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg">公告类型</param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void DELGYSGLBYID(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            try
            {
                if (!new KZ_GYSGLB().Delete(d => d.ID.ToString() == P1)) {

                    msg.ErrorMsg = "删除失败";
                }
            }
            catch (Exception ex)
            {
                msg.ErrorMsg = ex.Message;
            }
        }
        
    }
}